package com.chen106106.sstest.common.redis;

import com.chen106106.sstest.model.User;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.apache.commons.lang.math.NumberUtils;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class RedisClient {
	private ShardedJedisPool shardedJedisPool;
	private Random random=new Random();
	public boolean setUserList(User user) {
		int size=2;
		ShardedJedis jedis  = null;
		try {
			jedis = shardedJedisPool.getResource();
			Long codeTemp=(long) user.getId();
			if (codeTemp!=null&&String.valueOf(codeTemp).length()==11) {
				String key=getUserKey(codeTemp);
				jedis.zadd(key,new Date().getTime(),codeTemp+"");
				Long setSize=jedis.zcard(key);
				if (setSize>size) {
					jedis.zremrangeByRank(key, 0, setSize-size-1);
				}
//				jedis.zrange(key, 0, -1);
			}
			return true;
		} catch (Exception e) {
			return false;
		}finally{
			shardedJedisPool.returnResource(jedis);
		}
	}
	public Long getRandomUserId(User user) {
		ShardedJedis jedis  = null;
		try {
			jedis = shardedJedisPool.getResource();
			String key=getUserKey((long)user.getId());
			Long setSize=jedis.zcard(key);
			if (setSize>1) {
				int index=random.nextInt(NumberUtils.toInt(setSize.toString())-1);
				LinkedHashSet<String> tempSet= (LinkedHashSet<String>) jedis.zrange(key, index, index+1);
				if (tempSet.size()>1) {
					try {
						for (String string : tempSet) {
							if (!string.equals(Long.toString((long)user.getId()))) {
								return NumberUtils.toLong(string);
							}
						}
					} catch (Exception e) {
						return null;
					}
				}
			}
			return null;
		} catch (Exception e) {
			return null;
		}finally{
			shardedJedisPool.returnResource(jedis);
		}
	}
	private String getUserKey(Long cityCode){
		StringBuilder sb=new StringBuilder("mov_random_userId_");
		Long codeTemp=cityCode;
		if (codeTemp!=null&&String.valueOf(codeTemp).length()==11) {
			sb.append(String.valueOf(codeTemp).substring(0,8));
		}else {
			sb.append("10101201");
		}
		return sb.toString();
		
	}
	public boolean setListByRedis(Long userId,String Name,String preName,long preId,int limitNo){
		ShardedJedis jedis  = null;
		try {
			jedis = shardedJedisPool.getResource();
			jedis.lpush("mov_prize_userId",userId+"");
			jedis.ltrim("mov_prize_userId", 0, limitNo-1);
			jedis.set("mov_prize_userId:"+userId+":preId:", preId+"");
			jedis.set("mov_prize_userId:"+userId+":preId:"+preId+":preName", Name+"&&"+preName);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}finally{
			shardedJedisPool.returnResource(jedis);
		}
	}
	
	public String getpreIdbyUserId(Long userId){
		ShardedJedis jedis  = null;
		try {
			jedis = shardedJedisPool.getResource();
			int preId = NumberUtils.toInt(jedis.get("mov_prize_userId:"+userId+":preId:"));
			return jedis.get("mov_prize_userId:"+userId+":preId:"+preId+":preName");
		} catch (Exception e) {
			return null;
		}finally{
			shardedJedisPool.returnResource(jedis);
		}
		
	}
	public List<Map<String,Object>> getListByRedis(){
		ShardedJedis jedis  = null;
		try {
			jedis = shardedJedisPool.getResource();
			List<String> list = jedis.lrange("mov_prize_userId", 0,-1);
			if(list==null||list.isEmpty()){
				return null;
			}
			List<Map<String,Object>> rlist = new ArrayList<Map<String,Object>>();
			for (String str : list) {
				 Map<String,Object> m = new HashMap<String, Object>();
				 Long userId = NumberUtils.toLong(str);
				 String result = getpreIdbyUserId(userId);
				 m.put("userId", userId);
				 String[] s = result.split("&&");
				 m.put("preName",s[1]);
				 m.put("nickname",s[0]);
				 rlist.add(m);
			}
			return rlist;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}finally{
			shardedJedisPool.returnResource(jedis);
		}

	}
	
	public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
		this.shardedJedisPool = shardedJedisPool;
	}
	
	
}